
import { buffer, stream } from "@kit.ArkTS";
import hilog from "@ohos.hilog";

export interface ReadFromBuffersOptions {
  buffers: Uint8Array[];
  errorAfter?: number;
}

export class ReadFromBuffers extends stream.Readable {
  private buffersToRead: Uint8Array[];
  private numBuffersRead = 0;

  private errorAfter: number;
  constructor(options: ReadFromBuffersOptions) {
    super();
    this.buffersToRead = options.buffers;
    this.errorAfter = typeof options.errorAfter === "number" ? options.errorAfter : -1;
  }

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  doRead(size: number) {
    if (this.errorAfter !== -1 && this.errorAfter === this.numBuffersRead) {
      throw new Error("Mock Error")
      return;
    }
    if (this.numBuffersRead >= this.buffersToRead.length) {
      return this.push(null);
    }
    const buf = this.buffersToRead[this.numBuffersRead++]
    return this.push(buf);
  }

  // emit(action,data){
  //   super.emit(action,data)
  // }
  //
  // push(data){
  //   super.push(data)
  // }
}
